﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Objects;
using System.Collections.ObjectModel;
using InspectionDAL;

namespace InspectionVehicule.Pages
{
    public partial class DefectuositeMajeure : PageBase
    {

        private Collection<InspectionDAL.DefectuositeMajeure> _donnees;

        public override object State
        {
            get
            {
                return _donnees;
            }
            set
            {
                _donnees = (Collection<InspectionDAL.DefectuositeMajeure>)value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                InitialiserDonnees();
                BindDefectuosite();
            }
            else
            {
                //if (_donnees == null)
                ExtraireDefectuosite();

                _donnees = EpurerDefectuosite(_donnees);
            }
        }

        private void BindDefectuosite()
        {
            grdDefectuosites.DisplayMode = 1;
            grdDefectuosites.DataSource = _donnees;
            grdDefectuosites.DataBind();
        }

        private Collection<InspectionDAL.DefectuositeMajeure> EpurerDefectuosite(Collection<InspectionDAL.DefectuositeMajeure> donnees)
        {
            if (donnees != null)
            {
                donnees = new Collection<InspectionDAL.DefectuositeMajeure>(
                    (from defectuosite in _donnees
                     where !String.IsNullOrEmpty(defectuosite.CodeLocalisation) &&
                           !String.IsNullOrEmpty(defectuosite.CodeComposant) &&
                           !String.IsNullOrEmpty(defectuosite.CodeDefectuosite)
                     select defectuosite).ToList());
            }

            return donnees;
        }

        private void InitialiserDonnees()
        {
            AssurerDixElement(ref _donnees);
            UpdateDescription();
        }        

        private void AssurerDixElement<T>(ref Collection<T> collection) where T : new()
        {
            if (collection == null)
                collection = new Collection<T>();

            for (int i = collection.Count; i < 10; i++)
            {
                T obj = new T();
                collection.Add(obj);
            }
        }

        protected void btnUpdateDescription_Click(object sender, EventArgs e)
        {
            ExtraireDefectuosite();
            UpdateDescription();
            BindDefectuosite();
        }

        private void UpdateDescription()
        {
            InspectionEntities data = new InspectionEntities();
            var descriptions = data.DescriptionDefectuosite.ToDictionary((x) => x.Code);
            foreach (var defectuosite in _donnees)
            {
                if (!String.IsNullOrEmpty(defectuosite.CodeLocalisation) &&
                    descriptions.ContainsKey(defectuosite.CodeLocalisation))
                {
                    defectuosite.DescriptionLocalisation = descriptions[defectuosite.CodeLocalisation].Description;
                }
                if (!String.IsNullOrEmpty(defectuosite.CodeComposant) &&
                    descriptions.ContainsKey(defectuosite.CodeComposant))
                {
                    defectuosite.DescriptionComposant = descriptions[defectuosite.CodeComposant].Description;
                }
                if (!String.IsNullOrEmpty(defectuosite.CodeDefectuosite) &&
                    descriptions.ContainsKey(defectuosite.CodeDefectuosite))
                {
                    defectuosite.DescriptionDefectuosite = descriptions[defectuosite.CodeDefectuosite].Description;
                }
            }
        }

        private void ExtraireDefectuosite()
        {
            AssurerDixElement(ref _donnees);
            for (int i = 0; i < 10; i++)
            {
                InspectionDAL.DefectuositeVehicule defectuosite = _donnees[i];
                defectuosite.CodeLocalisation = ObtenirValeur(grdDefectuosites.Rows[i], "txtCodeLocalisation");
                defectuosite.CodeComposant = ObtenirValeur(grdDefectuosites.Rows[i], "txtCodeComposant");
                defectuosite.CodeDefectuosite = ObtenirValeur(grdDefectuosites.Rows[i], "txtCodeDefectuosite");
            }
        }

        private string ObtenirValeur(GridViewRow gridViewRow, string textBoxName)
        {
            return ((TextBox)gridViewRow.FindControl(textBoxName)).Text;
        }

        protected override string EtapeEnCours
        {
            get {return "DefectuositeMajeure"; }
        }
    }
}